import { handleActions } from 'redux-actions';
import { GET_ALL_NETWORK, ADD_NETWORK, ADD_DEVICE_TO_NETWORK, UPDATE_NETWOERK_ID, MODIFY_DEVICE, REMOVE_DEVICE, MOVE_DEVICE, DELETE_NETWORK, SWITCH_VPN, REFRESH_VPN } from '../types/network';
const defaultState = {
    dataList: [],
}
/**handleActions 接受两个参数 第一个参数是多个reducer  第二个参数是初始化时state的状态值 */
export default handleActions({
    [GET_ALL_NETWORK](state, action) {
        return {
            ...state,
            dataList: action.payload.dataList
        }
    },
    [ADD_NETWORK](state, action) {
        let { dataList } = state;
        dataList.push(action.payload.data);
        return {
            ...state,
            dataList
        }
    },
    [ADD_DEVICE_TO_NETWORK](state, action) {
        let { dataList } = state;
        const deviceItem = action.payload.data;
        for (let item of dataList) {
            if (item.teamId == deviceItem.teamId) {
                item.members.push(deviceItem);
                break;
            }
        }
        return {
            ...state,
            dataList
        }
    },
    [UPDATE_NETWOERK_ID](state, action) {
        let { dataList } = state;
        const { data } = action.payload;
        for (let [i, item] of dataList.entries()) {
            if (item.teamId == data.teamId) {
                dataList[i] = data;
                break
            }
        }
        return {
            ...state,
            dataList
        }
    },
    [MODIFY_DEVICE](state, action) {
        let { dataList } = state;
        const { serialNumber, networkId, deviceName } = action.payload.data;
        for (let [i, item] of dataList.entries()) {
            if (item.teamId == networkId) {
                for (let [m, itemM] of item.members.entries()) {
                    if (itemM.mac == serialNumber) {
                        item.members[m].labelName = deviceName;
                        break
                    }
                }
                dataList[i] = item;
                break
            }
        }
        return {
            ...state,
            dataList
        }
    },
    [REMOVE_DEVICE](state, action) {
        let { dataList } = state;
        const { serialNumber, networkId } = action.payload.data;
        for (let [i, item] of dataList.entries()) {
            if (item.teamId == networkId) {
                for (let [m, itemM] of item.members.entries()) {
                    if (itemM.mac == serialNumber) {
                        item.members.splice(m, 1);
                        break
                    }
                }
                dataList[i] = item;
                break
            }
        }
        return {
            ...state,
            dataList
        }
    },
    [MOVE_DEVICE](state, action) {
        let { dataList } = state;
        const { networkId, tonetworkId, serialNumber } = action.payload.data;
        let moveItem = '';
        let orgIndex = 0;
        let toIndex = 0;
        for (let [i, item] of dataList.entries()) {
            if (item.teamId == networkId) {
                for (let [m, itemM] of item.members.entries()) {
                    if (itemM.mac == serialNumber) {
                        moveItem = itemM;
                        orgIndex = i;
                        dataList[i].members.splice(m, 1);
                    }
                }
            }
            if (item.teamId == tonetworkId) {
                toIndex = i
            }
        }
        //移动
        if (dataList[toIndex].members instanceof Array) {
            dataList[toIndex].members.push(moveItem);
        } else {
            dataList[toIndex].members = [moveItem]
        }
        //offline online属性处理
        if (moveItem.status == 0) {
            dataList[orgIndex].offDevicesNum--;
            dataList[toIndex].offDevicesNum++;
        } else {
            dataList[orgIndex].onDevicesNum--;
            dataList[toIndex].onDevicesNum++;
        }
        return {
            ...state,
            dataList
        }
    },
    [DELETE_NETWORK](state, action) {
        let { dataList } = state;
        const { teamId } = action.payload;
        for (let [i, item] of dataList.entries()) {
            if (item.teamId == teamId) {
                dataList.splice(i, 1);
                break
            }
        }
        return {
            ...state,
            dataList
        }
    },
    [SWITCH_VPN](state, action) {
        let { dataList } = state;
        let { networkId, serialNumber, result } = state;
        for (let [i, item] of dataList.entries()) {
            if (item.teamId == networkId) {
                for (let [m, itemM] of item.members.entries()) {
                    if (itemM.mac == serialNumber) {
                        if (result == 'success') {
                            item.members[m].status = itemM.status == 0 ? 1 : 0
                        }
                    }
                }
                dataList[i] = item;
                break
            }
        }
        return {
            ...state,
            dataList
        }
    },
    [REFRESH_VPN](state, action) {
        let { dataList } = state;
        const { itemRes, changeStatus } = action.payload.data;
        const { teamId, members } = itemRes;
        const { mac, status } = members && members[0];
        for (let [index, item] of dataList.entries()) {
            if (item.teamId == teamId) {
                let members_old = item.members;
                for (let [indexM, itemM] of members_old.entries()) {
                    if (itemM.mac == mac) {
                        members_old[indexM] = members[0];
                        if (changeStatus == 1) {
                            if (status == "1") {
                                dataList[index].onDevicesNum++;
                                dataList[index].offDevicesNum--;
                            } else {
                                dataList[index].onDevicesNum--;
                                dataList[index].offDevicesNum++;
                            }
                        }
                    }
                    dataList[index].members = members_old;
                    break;
                }
                break;
            }
        }
        return {
            ...state,
            dataList
        }
    }
}, defaultState)